<?php

/**
 * Représente une instance de doctrine
 *
 * @package cass
 * @author Mickaël CASTANHEIRO <mickael.castanheiro@gmail.com>
 * @version 1.0
 */

namespace cass;

use Doctrine\Common\ClassLoader,
 Doctrine\ORM\Configuration,
 Doctrine\ORM\EntityManager,
 Doctrine\Common\Cache\ApcCache,
 Entities\User, Entities\Address;

class Doctrine extends Singleton {

    protected $entityManager;

    /**
     * Initialise la connexion par Doctrine
     */
    protected function __construct() {
        require_once __PATH__ . Data::directories('doctrine') . '/vendor/doctrine-common/lib/Doctrine/Common/ClassLoader.php';

        // Set up class loading. You could use different autoloaders, provided by your favorite framework,
        // if you want to.
        $classLoader = new ClassLoader('DoctrineExtension', __PATH__ . Data::directories('doctrine'));
        $classLoader->register();
        $classLoader = new ClassLoader('Doctrine\ORM', __PATH__ . Data::directories('doctrine'));
        $classLoader->register();
        $classLoader = new ClassLoader('Doctrine\DBAL', __PATH__ . Data::directories('doctrine') . 'vendor/doctrine-dbal/lib/');
        $classLoader->register();
        $classLoader = new ClassLoader('Doctrine\Common', __PATH__ . Data::directories('doctrine') . 'vendor/doctrine-common/lib/');
        $classLoader->register();
        $classLoader = new ClassLoader('Symfony', __PATH__ . Data::directories('doctrine') . 'vendor/');
        $classLoader->register();
        $classLoader = new ClassLoader(null, __PATH__ . Data::directories('doctrine') . 'config/Entities/');
        $classLoader->register();
        $classLoader = new ClassLoader('Proxies', __PATH__ . Data::directories('doctrine') . 'config/Proxies/');
        $classLoader->register();

        // Set up caches
        $config = new Configuration;
        //$cache = new ApcCache;
        //$config->setMetadataCacheImpl($cache);
        $driverImpl = new \Doctrine\ORM\Mapping\Driver\YamlDriver(__PATH__ . Data::directories('doctrine') . 'config/yaml');
        //$driverImpl = $config->newDefaultAnnotationDriver(array(__PATH__.   Data::directories('doctrine'). '/config/Entities'));
        $config->setMetadataDriverImpl($driverImpl);
        //$config->setQueryCacheImpl($cache);
        // Proxy configuration
        $config->setProxyDir(__PATH__ . Data::directories('doctrine') . 'config/Proxies/');
        $config->setProxyNamespace('Proxies');
        $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);

        // Database connection information
        $connectionOptions = array(
            'driver' => 'pdo_mysql',
            'host' => Data::database('host'),
            'dbname' => Data::database('dbname'),
            'user' => Data::database('user'),
            'password' => Data::database('password')
        );

        // Create EntityManager
        $this->entityManager = EntityManager::create($connectionOptions, $config);
    }

    public function __call($_method, $_arguments) {
        return call_user_func_array(array($this->entityManager, $_method), $_arguments);
    }

}